﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using ParieurPro.DAL;
using ParieurPro.DAL.DatabaseModel;

namespace MyBet.Web.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class GetRanks
    {
        /// <summary>
        /// Get ranks of all users from a contest id
        /// </summary>
        /// <param name="contestId">Contest identifier</param>
        /// <returns></returns>
        [OperationContract]
        public List<RankObject> GetContestRanks(int contestId)
        {
            List<RankObject> rankList= new List<RankObject>();
            List<RankObject> rankViewList;
            int rank = 1;

            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                rankViewList = RankObject.GetRanksInDatabase(dbContext, contestId);
            }

            foreach (RankObject rnk in rankViewList)
            {
                rnk.Rank = rank;
                rankList.Add(rnk);
                rank++;
            }

            return rankList;
        }

        /// <summary>
        /// Get ranks of all users from a contest id
        /// </summary>
        /// <param name="customContestId">Contest identifier</param>
        /// <param name="eventId">Event identifier</param>
        /// <returns></returns>
        [OperationContract]
        public List<RankObject> GetCustomContestRanksFromEvent(int customContestId, int eventId, out ParieurProFault myFault)
        {
            List<RankObject> returnValue = null;
            myFault = null;
            try
            {
                using (MyBetDBEntities dbContext = new MyBetDBEntities())
                {
                    returnValue = RankObject.GetCustomContestRanksFromEvent(dbContext, customContestId, eventId);
                }  
            }
            catch (Exception ex)
            {
                myFault = new ParieurProFault() { FaultType = "Get rank service error" , Message = ex.ToString() };
                returnValue = null;
            }
            return returnValue;
        }

        /// <summary>
        /// Get events list with a rank from contest id
        /// </summary>
        /// <param name="customContestId">Contest identifier</param>
        /// <returns></returns>
        [OperationContract]
        public List<EventObject> GetEventsWithRankFromCustomContest(int customContestId)
        {
            List<EventObject> returnValue=null;
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                returnValue = RankObject.GetEventsWithRankFromCustomContestId(dbContext, customContestId);
            }
            return returnValue;
        }

        // Ajoutez des opérations supplémentaires ici et marquez-les avec [OperationContract]
    }
}
